package com.example.basesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basesystem.common.Result;
import com.example.basesystem.entity.SysUser;
import com.example.basesystem.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@Tag(name = "用户管理", description = "用户相关接口")
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询用户列表
     */
    @Operation(summary = "分页查询用户列表", description = "分页查询用户列表接口")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public Result<Page<SysUser>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        Page<SysUser> userPage = sysUserService.getUserPage(page, username, status);
        return Result.success(userPage);
    }

    /**
     * 根据ID查询用户
     */
    @Operation(summary = "根据ID查询用户", description = "根据ID查询用户接口")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> getById(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        // 不返回密码
        if (user != null) {
            user.setPassword(null);
        }
        return Result.success(user);
    }

    /**
     * 新增用户
     */
    @Operation(summary = "新增用户", description = "新增用户接口")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:add')")
    public Result<Object> add(@Validated @RequestBody SysUser user,
                             @Parameter(description = "角色ID列表") @RequestParam(required = false) List<Long> roleIds) {
        boolean result = sysUserService.addUser(user, roleIds);
        return result ? Result.success() : Result.error("新增用户失败");
    }

    /**
     * 修改用户
     */
    @Operation(summary = "修改用户", description = "修改用户接口")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<Object> update(@Validated @RequestBody SysUser user,
                                @Parameter(description = "角色ID列表") @RequestParam(required = false) List<Long> roleIds) {
        boolean result = sysUserService.updateUser(user, roleIds);
        return result ? Result.success() : Result.error("修改用户失败");
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户", description = "删除用户接口")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<Object> delete(@PathVariable Long id) {
        boolean result = sysUserService.deleteUser(id);
        return result ? Result.success() : Result.error("删除用户失败");
    }

    /**
     * 重置密码
     */
    @Operation(summary = "重置密码", description = "重置密码接口")
    @PutMapping("/resetPwd/{id}")
    @PreAuthorize("hasAuthority('sys:user:resetPwd')")
    public Result<Object> resetPassword(@PathVariable Long id,
                                       @Parameter(description = "新密码") @RequestParam String password) {
        boolean result = sysUserService.resetPassword(id, password);
        return result ? Result.success() : Result.error("重置密码失败");
    }

    /**
     * 分配用户角色
     */
    @Operation(summary = "分配用户角色", description = "分配用户角色接口")
    @PostMapping("/assign-role/{id}")
    @PreAuthorize("hasAuthority('sys:user:assign-role')")
    public Result<Object> assignRole(@PathVariable Long id,
                                    @Parameter(description = "角色ID列表") @RequestBody List<Long> roleIds) {
        boolean result = sysUserService.assignUserRole(id, roleIds);
        return result ? Result.success() : Result.error("分配用户角色失败");
    }
}